//上传
/**
 * @param {string} filePath //上传文件路径
 * @param {string} cloudPath //上传文件名
 * @param {functin} callbackProgress 回调上传进度
 **/
async function upload(filePath,cloudPath,callbackProgress) {
	return new Promise(async(reslove,reject)=>{
		const result = await uniCloud.uploadFile({
			filePath: filePath,
			cloudPath: cloudPath,
			onUploadProgress: function(progressEvent) {
				var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent
					.total);
				callbackProgress&&callbackProgress(percentCompleted)
				// that.$refs.fileUpload.setProgress(percentCompleted, true)
			}
		});
		// that.$refs.fileUpload.setProgress(0, false)
		if (result.success) {
			//上传成功后把资源写到数据库，防止重复上传（后面可以把未使用的图片从云存储中删除）
			waiterfile(result.fileID, 'add')
			
			reslove(result.fileID)
			// data.file.url = result.fileID
			// that.dialog.form.fileId = result.fileID
			// that.$refs.fileUpload.setFileList([data.file])
			
		} else {
			reject()
			// that.$refs.fileUpload.setFileList([])
		}
	})
	
}
// 上传成功后把资源写到数据库，防止重复上传（后面可以把未使用的图片从云存储中删除）
//type为add添加数据，update更新
function waiterfile(fileId, type) {
	let req = {
		params: {
			type,
			fileId,
		},
		active: 'uploadFile',
		showLoad: false
	}
	uni.$lizhao.doCloud('filelist-cloud', req).then(res => {
		if (type == 'update' && res.code == 0) {
			//更新成功后，把之前上传的无用的图片从数据库和云存储中删除，防止资源浪费
			that.delectFile()
		}
	})
}
//提交成功后，把之前上传的无用的图片从数据库和云存储中删除，防止资源浪费
function delectFile() {
	let req = {
		active: 'delectFile',
		showLoad: false
	}
	uni.$lizhao.doCloud('filelist-cloud', req).then(res => {
		console.log(res);
	})
}
export default {
	upload,
	delectFile//删除无用资源
}